package chemtools_logica.BDL.SFBean;

import chemtools_logica.Entidades.Detalleformula;

import chemtools_logica.Entidades.DetalleformulaPK;

import chemtools_logica.BDL.IL.BDL_SFDetalleformulaLocal;
import chemtools_logica.BDL.IR.BDL_SFDetalleformulaRemote;

import chemtools_logica.Beans.DetalleformulaBean;

import java.util.List;

import javax.annotation.Resource;

import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "BDL_SFDetalleformula", mappedName = "map-BDL_SFDetalleformula")
public class BDL_SFDetalleformulaBean implements BDL_SFDetalleformulaRemote, BDL_SFDetalleformulaLocal {
    @Resource
    SessionContext sessionContext;
    @PersistenceContext(unitName = "Chemtools_Logica")
    private EntityManager em;

    public BDL_SFDetalleformulaBean() {
    }

    public Detalleformula persistDetalleformula(Detalleformula detalleformula) {
        em.persist(detalleformula);
        return detalleformula;
    }

    public Detalleformula mergeDetalleformula(Detalleformula detalleformula) {
        System.out.print("HOLA QUE HACE mergeeeeeeeeeeee");
        return em.merge(detalleformula);
    }

    public void removeDetalleformula(Detalleformula detalleformula) {
        detalleformula =
            em.find(Detalleformula.class,
                    new DetalleformulaPK(detalleformula.getFormula().getIdFormula(),
                                         detalleformula.getTipoinsumo().getIdTipoInsumo(),
                                         detalleformula.getTipoproducto().getIdTipoProducto()));
        em.remove(detalleformula);
    }

    /** <code>select o from Detalleformula o</code> */
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<Detalleformula> getDetalleformulaFindAll() {
        return em.createNamedQuery("Detalleformula.findAll", Detalleformula.class).getResultList();
    }

    
    public List<Detalleformula> buscaxIdFormula(int idFormula) {
        String ejbQuery = "select u from Detalleformula u where u.formula.idFormula="+idFormula;
        return em.createQuery(ejbQuery,Detalleformula.class).getResultList();
    }
    
    
    public boolean hasFormula(int idTipoAlmacen){
        boolean tof= false;
        
        String ejb = "SELECT u FROM Detalleformula u WHERE u.tipoproducto.idTipoProducto = "+idTipoAlmacen;
        
        try{
            Query query = em.createQuery(ejb);
            List<Detalleformula> dt = query.getResultList();
            
            if(dt.size()!=0){
                tof= true;
            }
        }catch(Exception e){}
        
        
        return tof;
    }
    
    public boolean productoTieneFormulaActiva(int idTipoProduco){
            String ejb = "SELECT u FROM Detalleformula u,Formula f WHERE f.estado=1 " +
                "and f.idFormula = u.formula.idFormula " +
                "and u.tipoproducto.idTipoProducto = "+idTipoProduco;
            return em.createQuery(ejb).getResultList().size()>0;
        }
    
    
}
